<?php

/**
 * This is the model class for table "{{project}}".
 *
 * The followings are the available columns in table '{{project}}':
 * @property integer $pro_id
 * @property string $pro_title
 * @property integer $pro_cid
 * @property string $pro_tags
 * @property integer $pro_created
 * @property string $pro_introduce
 * @property integer $pro_hits
 * @property integer $pro_uid
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Solution[] $solutions
 * @property Category $category
 */
class Project extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{project}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pro_title, pro_cid, pro_tags, pro_introduce', 'required'),
			array('pro_cid, pro_created, pro_hits, pro_uid', 'numerical', 'integerOnly'=>true),
			array('pro_tags, pro_title', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('pro_id,pro_cid, pro_title,  pro_tags, pro_created, pro_introduce, pro_hits, pro_uid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'pro_uid'),
			'solutions' => array(self::HAS_MANY, 'Solution', 'sol_pid'),
			'category'=>array(self::BELONGS_TO, 'Category', 'pro_cid'),
			'comment'=>array(self::HAS_MANY, 'comment', 'com_proid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'pro_id' => 'Pro',
			'pro_title' => 'Pro Title',
			'pro_cid' => 'Pro Cid',
			'pro_tags' => 'Pro Tags',
			'pro_created' => 'Pro Created',
			'pro_introduce' => 'Pro Introduce',
			'pro_hits' => 'Pro Hits',
			'pro_uid' => 'Pro Uid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('pro_id',$this->pro_id);
		$criteria->compare('pro_cid', $this->pro_cid);
		$criteria->compare('pro_title',$this->pro_title, true);
		$criteria->compare('pro_tags',$this->pro_tags,true);
		$criteria->compare('pro_created',$this->pro_created);
		$criteria->compare('pro_introduce',$this->pro_introduce,true);
		$criteria->compare('pro_hits',$this->pro_hits);
		$criteria->compare('pro_uid',$this->pro_uid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//获取上个项目
	public function getPreviewProject($id)
	{
		$model = self::model()->find('pro_id < :pro_id ORDER BY pro_id DESC',array(':pro_id'=>$id));
		return $model;
	}
	
	//获取下一个项目
	public function getNextProject($id)
	{
		$model = self::model()->find('pro_id > :pro_id ORDER BY pro_id ASC',array(':pro_id'=>$id));
		return $model;		
	}
	
	//获取项目简要介绍
	public function getBrifeDescript($model, $length=15)
	{
		$string = $model->pro_introduce;
		
		$string =  strlen(strip_tags($string))?strip_tags($string):date('y-m-d',$preview->pro_created);
		
		$string = UtilTools::strSlice($string,0,$length);
		
		return $string;
	}
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->pro_created = time();
				$this->pro_uid = Yii::app()->user->id;
				
				$this->pro_hits = 0;
			}

			return true;
		}
		else
			return false;
	}
	
}